import { Module } from '@nestjs/common';
import { AuthService } from './auth.service';
import { AuthController } from './auth.controller';
import { UserModule } from '../user/user.module';
import { RoleModule } from '../role/role.module';
import { JwtModule, JwtService } from '@nestjs/jwt';
// import { JwtStrategy } from './jwt.strategy';
import { RefreshJwtStrategy } from './refresh-jwt.strategy';
import { PassportModule } from '@nestjs/passport';
import { AppConfigService } from '../../config/config.service';
import { JwtAuthGuard } from './guards/jwt-auth.guard';
import { APP_GUARD } from '@nestjs/core';
import { CacheModule } from 'src/cache/cache.module';

@Module({
  imports: [
    UserModule,
    PassportModule,
    RoleModule,
    JwtModule.registerAsync({
      global: true,
      useFactory: () => ({
        secret: process.env.JWT_SECRET,
        signOptions: { expiresIn: '3m' },
      }),
    }),
    CacheModule,
  ],
  controllers: [AuthController],
  providers: [
    AuthService,
    AppConfigService,
    {
      provide: APP_GUARD,
      useClass: JwtAuthGuard,
    },
    // JwtStrategy,
    RefreshJwtStrategy, // 自定义RefreshToken的JwtService
    {
      provide: 'REFRESH_JWT_SERVICE',
      useFactory: (configService: AppConfigService) => {
        return new JwtService({
          secret: configService.jwtRefreshSecret,
          signOptions: { expiresIn: '2d' },
        });
      },
      inject: [AppConfigService],
    },
  ],
  exports: [AuthService],
})
export class AuthModule {}
